package com.leetcode.Offer;

public class JZ26 {

    public boolean isSubStructure(TreeNode A, TreeNode B) {//寻找起点

        boolean result = false;
        if (A != null && B != null) {
            if (A.val == B.val) {
                result = isSubStructure2(A, B);
            }
            if (!result) {
                result = isSubStructure(A.left, B);
            }
            if (!result) {
                result = isSubStructure(A.right, B);
            }
        }
        return result;
    }

    private boolean isSubStructure2(TreeNode A, TreeNode B) {//对比子树

        if (B == null) {
            return true;
        }
        if (A == null) {
            return false;
        }

        if (A.val != B.val) {
            return false;
        }
        return isSubStructure2(A.left, B.left) && isSubStructure2(A.right, B.right);
    }

}
